package com.winhxd.b2c.order.api;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.context.CustomerUser;
import com.winhxd.b2c.common.context.UserContext;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.*;
import com.winhxd.b2c.common.domain.order.enums.AfterSaleTypeEnum;
import com.winhxd.b2c.common.domain.order.model.OrderInfo;
import com.winhxd.b2c.common.domain.order.vo.OrderReadVO;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.order.adapter.AbstractAfterSaleAdapter;
import com.winhxd.b2c.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author hjw
 * @date 2019/01/10 11:16
 */
@RestController
@Api(tags = "ApiOrder")
@RequestMapping(value = "/api-order/order")
public class ApiOrderController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApiOrderController.class);

    @Resource
    public OrderService orderService;

    @ApiOperation(value = "C端订单确认收货", notes = "订单确认收货")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.ORDER_STATUS_CHANGE_FAILURE, message = "订单状态修改失败"),
            @ApiResponse(code = BusinessCode.ORDER_DOES_NOT_EXIST, message = "订单不存在"),
            @ApiResponse(code = BusinessCode.CODE_440601, message = "订单已自动确认收货，不能再进行任何操作"),
            @ApiResponse(code = BusinessCode.CODE_405805, message = "订单有未完成售后")
    })
    @RequestMapping(value = "/4407/v1/orderConfirm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Deprecated
    public ResponseResult<Void> orderConfirm(@RequestBody OrderBaseConditions orderBaseConditions) {
        String logTitle = "/4407/v1/orderConfirm-订单确认收货";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(orderBaseConditions));
        ResponseResult<Void> result = new ResponseResult<>();
        orderService.manualReceived(orderBaseConditions);
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }

    @ApiOperation(value = "C端订单确认收货", notes = "订单确认收货")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.ORDER_STATUS_CHANGE_FAILURE, message = "订单状态修改失败"),
            @ApiResponse(code = BusinessCode.ORDER_DOES_NOT_EXIST, message = "订单不存在"),
            @ApiResponse(code = BusinessCode.CODE_440601, message = "订单已自动确认收货，不能再进行任何操作"),
            @ApiResponse(code = BusinessCode.CODE_405805, message = "订单有未完成售后")
    })
    @RequestMapping(value = "/4407/v2/orderConfirm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Void> orderConfirmV2(@RequestBody OrderBaseConditions orderBaseConditions) {
        String logTitle = "/4407/v2/orderConfirm-订单确认收货";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(orderBaseConditions));
        ResponseResult<Void> result = new ResponseResult<>();
        orderService.manualReceivedv2(orderBaseConditions);
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }

    @ApiOperation(value = "C端订单发起售后申请", notes = "发起售后申请")
    @ApiResponses({
            @ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.ORDER_STATUS_CHANGE_FAILURE, message = "订单状态修改失败"),
            @ApiResponse(code = BusinessCode.CODE_440601, message = "订单已自动确认收货，不能在进行任何操作！！"),
            @ApiResponse(code = BusinessCode.ORDER_DOES_NOT_EXIST, message = "订单不存在")
    })
    @RequestMapping(value = "/4406/v1/orderStartAfterSale", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Deprecated
    public ResponseResult<Void> orderStartAfterSale(@RequestBody OrderStartAfterSaleCondition orderStartAfterSaleCondition) {
        String logTitle = "/4406/v1/orderStartAfterSale-发起售后申请";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(orderStartAfterSaleCondition));
        ResponseResult<Void> result = new ResponseResult<>();
        //根据售后类型发起售后申请
        AbstractAfterSaleAdapter afterSale = AbstractAfterSaleAdapter.getAfterSaleAdapter(AfterSaleTypeEnum.getAfterSaleTypeEnumByCode(orderStartAfterSaleCondition.getAfterSaleType()));
        afterSale.execute(orderStartAfterSaleCondition);
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }

    @ApiOperation(value = "预订单", notes = "预订单")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1002, message = "登录凭证无效")
    })
    @RequestMapping(value = "/4001/v1/readyOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<OrderReadVO> readyOrder(@RequestBody OrderReadyCondition condition) {
        String logTitle = "/api-order/order/4001/v1/readyOrder-预订单";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        ResponseResult<OrderReadVO> result = new ResponseResult<>();

        CustomerUser customer = UserContext.getCurrentCustomerUser();
        if(null == customer||null == customer.getCustomerId()){
            LOGGER.error(logTitle+"--用户为空！");
            throw new BusinessException(BusinessCode.CODE_440101);
        }

        OrderReadVO orderReadVO = orderService.readyOrder(customer,condition);
        result.setData(orderReadVO);

        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }

    @ApiOperation(value = "创建订单", notes = "创建订单")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1002, message = "登录凭证无效")
    })
    @RequestMapping(value = "/4002/v1/createOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<OrderInfo> createOrder(@RequestBody OrderCreateCondition condition) {
        String logTitle = "/api-order/order/4002/v1/createOrder-创建订单";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        ResponseResult<OrderInfo> result = new ResponseResult<>();

        CustomerUser customer = UserContext.getCurrentCustomerUser();
        if(null == condition||null == condition.getCustomerId()){
            LOGGER.error(logTitle+"--用户为空！");
            throw new BusinessException(BusinessCode.CODE_401001);
        }

        OrderInfo orderInfo = orderService.createOrder(customer,condition);
        result.setData(orderInfo);

        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }

    @ApiOperation(value = "取消订单", notes = "取消订单")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1002, message = "登录凭证无效")
    })
    @RequestMapping(value = "/4003/v1/cancelOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Void> cancelOrder(@RequestBody OrderCancelCondition condition) {
        String logTitle = "/api-order/order/4003/v1/cancelOrder-取消订单";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        ResponseResult<Void> result = new ResponseResult<>();

        CustomerUser customer = UserContext.getCurrentCustomerUser();
        if(null == customer||null == customer.getCustomerId()){
            LOGGER.error(logTitle+"--用户为空！");
            throw new BusinessException(BusinessCode.CODE_440101);
        }

        orderService.cancelOrderByCustomer(customer,condition);

        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }
}
